package com.zsc.eb.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.eb.model.EbNews;
import com.zsc.eb.model.EbProduct;
import com.zsc.eb.model.EbProductCategory;
import com.zsc.eb.service.EbNewsService;
import com.zsc.eb.service.EbProductCategorySercie;
import com.zsc.eb.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller//证
//工号
@RequestMapping("/front")
public class indexController {

    @Autowired
    private EbProductService ebProductService;

    @Autowired
    private EbNewsService ebNewsService;

    @Autowired
    private EbProductCategorySercie ebProductCategorySercie;

    //行为 返回首页的数据(model)和视图(view-html)
    @RequestMapping("/index")//返回前端首页的数据和视图
    public String index(Model model, HttpSession session,
                        @RequestParam(value = "pSize",defaultValue = "1") Integer pSize,
                        @RequestParam(value = "pageSize",defaultValue = "8")Integer pageSize){
        //1.调用service层查询分页的商品信息
        Page<EbProduct> ebProductPage = ebProductService.selectList(pSize, pageSize);
        //2.调用service层查询分页的新闻信息
        List<EbNews> ebNewsList = ebNewsService.selectList(1, 7);


        //3.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategorySercie.selectBigAndSmallCate();
        //放到session域中
        //session.setAttribute("categoryMap",categoryMap);


        //4.调用service查询商品的最大页数
        int maxPage = ebProductService.getMaxPage(pageSize);

        //4.将数据绑定model中  key-value进行数据绑定
        model.addAttribute("ebProductList",ebProductPage.getRecords());
        model.addAttribute("ebNewsList",ebNewsList);
        model.addAttribute("categoryMap",categoryMap);

        //绑定当前页
        model.addAttribute("curPage",pSize);
        //是否有下一页  ebProductPage.hasNext()//判断是否还有下一页
        model.addAttribute("ebProductPage",ebProductPage);
        //绑定最大页数
        model.addAttribute("maxPage",maxPage);
        //5.返回视图(html)
        return "index";//templates/index.html

    }


}
